/**
 * 
 */
package cn.tonyqiu.wonuly.web.impl;

import java.util.ArrayList;
import java.util.List;

import org.gwtwidgets.server.spring.GWTSpringController;

import cn.tonyqiu.wonuly.client.rpc.BillService;
import cn.tonyqiu.wonuly.client.vo.BillVo;
import cn.tonyqiu.wonuly.model.Bill;
import cn.tonyqiu.wonuly.service.BillManager;
import cn.tonyqiu.wonuly.service.LookupManager;
import cn.tonyqiu.wonuly.service.UserManager;
import cn.tonyqiu.wonuly.web.util.DateFormatter;

/**
 * @author <a href="mailto:tony.qiu.smart@gmail.com">Tony Qiu</a>
 * 
 */
public class BillServiceImpl extends GWTSpringController implements BillService {

	private BillManager billManager;
	private LookupManager lookupManager;
	private UserManager userManager;

	/**
	 * @return the userManager
	 */
	public UserManager getUserManager() {
		return userManager;
	}

	/**
	 * @param userManager the userManager to set
	 */
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	/**
	 * @return the lookupManager
	 */
	public LookupManager getLookupManager() {
		return lookupManager;
	}

	/**
	 * @param lookupManager the lookupManager to set
	 */
	public void setLookupManager(LookupManager lookupManager) {
		this.lookupManager = lookupManager;
	}

	/**
	 * @return the billManager
	 */
	public BillManager getBillManager() {
		return billManager;
	}

	/**
	 * @param billManager
	 *            the billManager to set
	 */
	public void setBillManager(BillManager billManager) {
		this.billManager = billManager;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.tonyqiu.wonuly.client.rpc.WonulyService#getBillsByUser()
	 */
	@Override
	public List<BillVo> getBillsByUser() throws Exception {
		List<Bill> bills = billManager.getBillsByUser();
		List<BillVo> returnBills = new ArrayList<BillVo>();
		BillVo bill;
		for (int i = 0; i < bills.size(); i++) {
			bill = new BillVo();
			bill.setId( bills.get(i).getId().toString());
			bill.setCategory(bills.get(i).getCategory().getName());
			bill.setAmount(bills.get(i).getAmount() + "");
			bill.setDate(DateFormatter.formatDateShorter(bills.get(i).getDate()));
			bill.setComment(bills.get(i).getComment());
			returnBills.add(bill);
		}

		// System.out.println(returnBills);
		return returnBills;

	}

	/* (non-Javadoc)
	 * @see cn.tonyqiu.wonuly.client.rpc.BillService#AddBill()
	 */
	
	public BillVo addBill(BillVo bill) throws Exception {
		
		Bill toAddBill = new Bill();
		
		toAddBill.setAccount(lookupManager.getAccountByAccountName(bill.getAccount()));
		toAddBill.setCategory(lookupManager.getCategoryByCategoryName(bill.getCategory()));
		toAddBill.setAmount(Float.parseFloat(bill.getAmount()));
		toAddBill.setDate(DateFormatter.parse(bill.getDate()));
		toAddBill.setUserId(userManager.getUserByEmail().getId());
		toAddBill.setComment(bill.getComment());
		toAddBill.setParent(-1);
		Bill returnedBill = billManager.save(toAddBill);
		bill.setId(returnedBill.getId().toString());
		return bill;
	}

}
